﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FCTC_Web.Models;
using FCTC_Web.DAL;
using DAL;
using FCTC_Web.Utilities;

namespace FCTC_Web.Controllers.Maintains
{
    [HandleError]
    public class AppointmentMaintainController : Controller
    {

        public ViewResult Index(int? pageId)
        {
            return View("Index", Pager6.Ins.GetPage<VehicleEstimation>(pageId, "Vehicle", "Personal"));
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            CH.Delete<VehicleEstimation>(id);
            var pageId = Pager6.Ins.GetPageId<VehicleEstimation>(id);
            return RedirectToAction("Index", new { pageId = pageId });
        }

        [HttpPost]
        public ActionResult Edit(VehicleEstimation ve, string date, Personal p, string VehicleID)
        {

            ModelState.Remove("CarBrand");
            ModelState.Remove("BrandType");
            ModelState.Remove("VehicleStyle");
            ModelState.Remove("ContactNumber");
            ve.VehicleID = Convert.ToInt32(VehicleID);
            if (ModelState.IsValid)
            {
                CH.Edit<Personal>(p);
                ve.PersonalID = p.ID;
                CH.Edit<VehicleEstimation>(ve);
                var pageId = Pager6.Ins.GetPageId<VehicleEstimation>(ve.ID);
                return RedirectToAction("Index", new { pageId = pageId });
            }
            InitialDDL(ve);
            return View(ve);
        }


        public ViewResult Details(int id)
        {
            VehicleEstimation ve = CH.GetDataById<VehicleEstimation>(id);
            return View(ve);
        }

        private void InitialDDL(VehicleEstimation ve = null)
        {
            if (ve == null)
            {
                ViewBag.EstimatorID = DrapDownListHelper.InitialSelectionDropDownList<Estimator>("ID", "ID", "Personal.FullName");
                return;
            }
            ViewBag.EstimatorID = DrapDownListHelper.InitialSelectionDropDownList<Estimator>("ID", "ID", "Personal.FullName", ve.EstimatorID);
        }

        public ActionResult Edit(int id)
        {
            VehicleEstimation ve = CH.GetDataById<VehicleEstimation>(id);
            var p = CH.GetDataById<Personal>(ve.PersonalID);
            ve.Personal = p;
            InitialDDL(ve);
            return View(ve);
        }

        public ActionResult Delete(int id)
        {
            return View(CH.GetDataById<VehicleEstimation>(id));
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}